#include "Algorithm.h"


Algorithm::Algorithm(SystemView* Sys, int sort_type, Writer* w, Parser* prs)
{
	sys = Sys;
	timeSliceCalc = new TimeSliceCalculations(sys);
	parser = prs;
	sorter = new Sorter(sys->getMy_luns(), sort_type);
	writer = w;
	ssdExtents = new vector<ExtentStatsAlgo*>();
	fcExtents = new vector<ExtentStatsAlgo*>();
	sataExtents = new vector<ExtentStatsAlgo*>();
	all_damages= new map<int, double>();
	sortedExtents = new map<string, ExtentStatsAlgo*>();
	sortedAlgo = new vector<ExtentStatsAlgo*>();
	ssd = sys->getDevice("SSD");
	fc = sys->getDevice("FC");
	sata = sys->getDevice("SATA");
	ssdDivisionPercGroup1 = new map<int,double>();
	fcDivisionPercGroup1 = new map<int,double>();
	sataDivisionPercGroup1 = new map<int,double>();
	ssdDivisionPercGroup2 = new map<int,double>();
	fcDivisionPercGroup2 = new map<int,double>();
	sataDivisionPercGroup2 = new map<int,double>();
	ssdDivisionPercGroup3 = new map<int,double>();
	fcDivisionPercGroup3 = new map<int,double>();
	sataDivisionPercGroup3 = new map<int,double>();

	out.open("E:/InputFiles/ExtentPlacements/ExtentPlacements_" + writer->outputFilesName, ios::app);
	outNoPriority.open("E:/InputFiles/ExtentPlacements/ExtentPlacementsNoPriority_" + writer->outputFilesName, ios::app);
}


Algorithm::~Algorithm(void)
{
	ssdDivisionPercGroup1->clear();
	ssdDivisionPercGroup2->clear();
	ssdDivisionPercGroup3->clear();
	fcDivisionPercGroup1->clear();
	fcDivisionPercGroup2->clear();
	fcDivisionPercGroup3->clear();
	sataDivisionPercGroup1->clear();
	sataDivisionPercGroup2->clear();
	sataDivisionPercGroup3->clear();
	delete sorter;
	delete timeSliceCalc;
	delete sortedExtents;
	delete sortedAlgo;
	delete all_damages;
	delete ssdDivisionPercGroup1;
	delete ssdDivisionPercGroup2;
	delete ssdDivisionPercGroup3;
	delete fcDivisionPercGroup1;
	delete fcDivisionPercGroup2;
	delete fcDivisionPercGroup3;
	delete sataDivisionPercGroup1;
	delete sataDivisionPercGroup2;
	delete sataDivisionPercGroup3;

	out.close();
	outNoPriority.close();
}


/*
time	device type		lun ID		extent offset
*/
void Algorithm::WritePlacements(vector<ExtentStatsAlgo*> *ssdExtents, vector<ExtentStatsAlgo*> *fcExtents, vector<ExtentStatsAlgo*> *sataExtents, int time){
	cout<<"time in placements is: "<<time<<endl;
	vector<ExtentStatsAlgo*>::iterator iter;
	
	iter=ssdExtents->begin();
	while (iter!=ssdExtents->end()){
		out<<time<<"\tssd\t"<<(*iter)->getExtent()->getLun()->getM_uid()<<"\t"<<(*iter)->getExtent()->my_offset<<endl;
		iter++;
	}

	iter=fcExtents->begin();
	while (iter!=fcExtents->end()){
		out<<time<<"\tfc\t"<<(*iter)->getExtent()->getLun()->getM_uid()<<"\t"<<(*iter)->getExtent()->my_offset<<endl;
		iter++;
	}

	iter=sataExtents->begin();
	while (iter!=sataExtents->end()){
		out<<time<<"\tsata\t"<<(*iter)->getExtent()->getLun()->getM_uid()<<"\t"<<(*iter)->getExtent()->my_offset<<endl;
		iter++;
	}
}

/*
time	device type		lun ID		extent offset
*/
void Algorithm::WritePlacementsNoPriority(vector<ExtentStatsAlgo*> *ssdExtents, vector<ExtentStatsAlgo*> *fcExtents, vector<ExtentStatsAlgo*> *sataExtents, int time){
	cout<<"time in placements NO PRIORITY is: "<<time<<endl;
	vector<ExtentStatsAlgo*>::iterator iter;
	
	iter=ssdExtents->begin();
	while (iter!=ssdExtents->end()){
		outNoPriority<<time<<"\tssd\t"<<(*iter)->getExtent()->getLun()->getM_uid()<<"\t"<<(*iter)->getExtent()->my_offset<<endl;
		iter++;
	}

	iter=fcExtents->begin();
	while (iter!=fcExtents->end()){
		outNoPriority<<time<<"\tfc\t"<<(*iter)->getExtent()->getLun()->getM_uid()<<"\t"<<(*iter)->getExtent()->my_offset<<endl;
		iter++;
	}

	iter=sataExtents->begin();
	while (iter!=sataExtents->end()){
		outNoPriority<<time<<"\tsata\t"<<(*iter)->getExtent()->getLun()->getM_uid()<<"\t"<<(*iter)->getExtent()->my_offset<<endl;
		iter++;
	}
}

